package br.com.lvc.bicho.standartgame;

import java.util.Locale;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageButton;
import br.com.lvc.bicho.AnimalButton;
import br.com.lvc.bicho.AnimalGameActivity;
import br.com.lvc.bicho.AnimationController;
import br.com.lvc.bicho.R;

public class MainActivity extends AnimalGameActivity implements OnInitListener {


	private ImageButton imageButtonSpain;
	private ImageButton imageButtonEua;
	private ImageButton imageButtonRoar;

	private static final int SPAIN = 0;
	private static final int EUA = 1;
	private static final int ROAR = 2;

	private static final int INTENT_TEXT_SPEECH_CODE = 9;
	private TextToSpeech textSpeech = null;
	private String textoQueSeraFalado = null;

	private boolean roar = true;
	private Locale selectLocale = Locale.US;
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		imageButtonEua = (ImageButton)findViewById(R.id.buttonEua);
		imageButtonSpain = (ImageButton)findViewById(R.id.buttonSpain); 
		imageButtonRoar = (ImageButton)findViewById(R.id.buttonRoar);
	}

	@Override
	protected boolean startAnimationOnInit() {
		return true;
	}

	private void startAnimationPressed(View view) {
		cancelAnimationOffButton(imageButtonEua,imageButtonRoar,imageButtonSpain);
		Animation animation = AnimationController.createAnimationAlphaNormally();
		view.startAnimation(animation);
		view.setAnimation(animation);
	}

	private void cancelAnimationOffButton(ImageButton...animalButtons) {
		for(ImageButton animalButton : animalButtons) {
			Animation animation = animalButton.getAnimation();
			if (animation != null) {
				animation.setRepeatCount(0);
				animation.reset();
			}
		}
	}

	public void onClickRoar(View view) {
		actionRoar();
		startAnimationPressed(view);
	}

	public void onClickSpain(View view) {
		actionSpain();
		startAnimationPressed(view);
	}

	public void onClickEua(View view) {
		actionEUA();
		startAnimationPressed(view);
	}

	public void configureLayout()  {
		if(!isTablet())
			findViewById(R.id.linear_botoon_layout).setVisibility(View.GONE);   		
	}

	public boolean onCreateOptionsMenu(android.view.Menu menu){
		if(!isTablet()) {
			super.onCreateOptionsMenu(menu);	
			MenuItem item;
			item = menu.add(0,SPAIN,0,"");
			item.setIcon(R.drawable.spain);
			item = menu.add(0,EUA,0,"");
			item.setIcon(R.drawable.eua);
			item = menu.add(0,ROAR,0,"");
			item.setIcon(R.drawable.speech);
		}

		return true;
	}

	public boolean onMenuItemSelected(int featuredID,MenuItem item) {
		boolean result = true;

		switch(item.getItemId()) {
		case SPAIN:
			actionSpain();
			break;
		case EUA:			
			actionEUA();
			break;
		case ROAR:
			actionRoar();
			break;
		}

		return result;
	}


	private void actionSpain() {
		selectLocale = new Locale("es", "ES");
		roar = false;
	}

	private void actionEUA() {
		selectLocale = Locale.US;
		roar = false;
	}

	private void actionRoar() {
		roar = true;
	}

	private  boolean isTablet() {
		return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;		 
	}

	public void pressed(AnimalButton animalButton) {
		if(roar) {
			playSound(animalButton);
		} else {
			textoQueSeraFalado = getString(animalButton.getResTextToSpeech());
			Intent checkIntent = new Intent();
			checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
			startActivityForResult(checkIntent, INTENT_TEXT_SPEECH_CODE);	
		}
	}

	private String getString(TextSpeech text) {
		if(selectLocale.equals(Locale.US)) 
			return getString(text.getResStringEn());
		else 
			return getString(text.getResStringES());
	}

	protected  AnimalButton createAnimalButton(int drawable,TextSpeech text,int sound) {
		return new AnimalButton(this,this,drawable, text,sound);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == INTENT_TEXT_SPEECH_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				textSpeech = new TextToSpeech(this, this);
			} else {
				DialogInterface.OnClickListener positiveEventAction = new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						installTextToSpeech();
						
					}
				};
		        
				showDialogTwoButton(R.string.attention, R.string.intall_text_to_speech, R.string.yes, R.string.no, this, positiveEventAction, null);
			}
		}

	}
	
	private void installTextToSpeech() {
		Intent installIntent = new Intent();
		installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
		startActivity(installIntent);
	}
	
	public void showDialogTwoButton(int title, int message,int labelButtonUm, int labelButtonDois, Context context, DialogInterface.OnClickListener positiveEventAction, DialogInterface.OnClickListener negativeEventAction) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);		
		builder.setMessage(context.getString(message));		
		builder.setPositiveButton(context.getString(labelButtonUm), positiveEventAction);
		builder.setNegativeButton(context.getString(labelButtonDois), negativeEventAction);
		AlertDialog dialog = builder.create();
		String titleDialog =  context.getString(title);
		dialog.setTitle(Html.fromHtml(titleDialog));
	
		dialog.show();
	}
	
	public void onInit(int status) {
		if(status == TextToSpeech.SUCCESS) {
			textSpeech.setLanguage(selectLocale);
			textSpeech.speak(textoQueSeraFalado, TextToSpeech.QUEUE_FLUSH, null);
		}
	}

}